use std::sync::{Arc, Mutex, Condvar};
use std::thread::{spawn, sleep};
use std::time::Duration;

fn main() {
    println!("Hello, world!");

    let flag = Arc::new(Mutex::new(false));
    let cond = Arc::new(Condvar::new());
    let cflag = flag.clone();
    let ccond = cond.clone();

    println!("step 1, {:?}", flag);
    let hdl = spawn(move || {
        let mut lock = cflag.lock().unwrap();
        let mut counter = 0;

        while counter < 3 {
            while !*lock {
                lock = ccond.wait(lock).unwrap();
            }
            //println!("step inner 1, {:?}", cflag);
            *lock = false;

            counter += 1;
            println!("inner counter:{counter}");
            // println!("inner counter2:{}", counter);
        }
    });
    println!("step 2, {:?}", flag);
    let mut counter = 0;
    loop {
        sleep(Duration::from_millis(1000));
        *flag.lock().unwrap() = true;
        counter += 1;
        if counter > 3 {
            break;
        }
        println!("ounter counter:{}", counter);
        cond.notify_one();
    }

    hdl.join().unwrap();
    println!("{:?}", flag);

}
